package com.ruoyi.framework.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.page.PageQO;
import com.ruoyi.common.utils.PageDUtils;

/**
 * aop环绕通知，在*service.list*()方法前后执行，与mybatis的PageHelper插件一起完成PageParams的初始化
 *
 * @author Administrator
 */
@Component
@Aspect
public class PageQOAspect {

    //@Around(value = "execution(* *..*Service.query*(..))")
    //@Around(value = "execution(* com.ruoyi.*..*Service.query*(..))")
    //@Around(value = "execution(* com.ruoyi.*..*.query*(..))")
    @Around(value = "execution(* com.ruoyi..*Service.query*(..))")
//    @Around(value = "execution(* com.ruoyi.biz..*Service.query*(..)) "
//    		+ " || execution(** com.ruoyi.crm..*Service.query*(..)) ")
    public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
        // 取出 PageQO
        Object[] args = joinPoint.getArgs();
        PageQO pageBean = null;
        for (Object obj : args) {
            if (obj instanceof PageQO) {
                pageBean = (PageQO) obj;
                break;
            }
        }

        // 如果分页，指定分页参数
        if (null != pageBean && pageBean.isPagination()) {
        	PageDUtils.startPage(pageBean);
            //PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
        }

        Object returnValue = joinPoint.proceed(args);

        // 如果分页，获得总记录数
//        if (null != pageBean && pageBean.isPagination() && null != returnValue &&
//                returnValue instanceof List) {
//            List list = (List) returnValue;
//            PageInfo pageInfo = new PageInfo(list);
//            Long total = pageInfo.getTotal();
//            pageBean.setTotal(total.intValue());
//        }

        return returnValue;
    }
}